<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
    <c:set var="ctx" value="${pageContext.request.contextPath}" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>修改厂商</title>
    <jsp:include page="../../import.jsp" flush="true" />
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div id="pageInTab" class="pageInTab">
        <div tabid="tabA" title="基本信息" lselected="true">
            <form id="formSet1">
            	<div class="fieldset1 tabel_col clearFix">
					<div class="caption">
                        <span class="title">基本信息</span>
                    </div>
					<div class="fieldBox col_50">
						<div class="label">厂商名称</div>
						<div class="field">
							<input name="mfrNm" id="mfrNm" type="text" class="ui-textbox" validate="{required:true}"> 
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">社会统一信用代码</div>
						<div class="field">
							<input name="succ" id="succ" type="text" class="ui-textbox" validate="{required:true}">
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">厂商地址</div>
						<div class="field">
							<input name="mfrAddr" id="mfrAddr" type="text" class="ui-textbox" value="">
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">办公电话</div>
						<div class="field">
							<input name="mfrTel" id="mfrTel" type="text" class="ui-textbox"  validate="{mobileAndFixedPhone:true}"
								value="">
						</div>
					</div>
					<div class="fieldBox col_50">
						<div class="label">
							<span class="sqiconfont sqicon-tishi c_sqYellow gridTip" title="说明：厂商金融业务放款时，用于判断放款金额是否扣除贴息费用项。"></span>放款方式
						</div>
						<div class="field">
							<select name="payTypCd" id="payTypCd" data-value=" " validate="{required:true}">
								<option value=" " checked >请选择</option>
								<c:forEach items="${payTypCdList }" var="kv">
									<option value="${kv.key }">${kv.value }</option>
								</c:forEach>
							</select>
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_66_auto">
						<div class="label">区域</div>
						<div class="field">
							<!-- <input name="rgnPrCd" id="rgnPrCd" type="text" class="ui-textbox" value=""> -->
							<jsp:include page="../../system/sysRegion/region.jsp"
										 flush="true">
								<jsp:param name="regionShowLvl" value="2" />
							</jsp:include>
						</div>
					</div>
					<div class="fieldBox col_33">
						<div class="label">经销品牌(卡车类)</div>
						<div class="field">
							<input type="text" id="tkBrCd" name="tkBrCd" ltype="combobox"
								   validate="{required:true}" />
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_33">
						<div class="label">经销品牌(乘用车类)</div>
						<div class="field">
							<input type="text" id="carBrCd" name="carBrCd" ltype="combobox"
								   validate="{required:true}" />
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_33">
						<div class="label">经销品牌(客车类)</div>
						<div class="field">
							<input type="text" id="busBrCd" name="busBrCd" ltype="combobox"
								   validate="{required:true}" />
							<span class="requiredSymbol">*</span>
						</div>
					</div>
					<div class="fieldBox col_100">
						<div class="label">其他信息</div>
						<div class="field" style="width: 100%;">
									<textarea name="otherInfo" id="otherInfo" rows="4"
											  class="l-textarea" style="width: 99%" value=""></textarea>
						</div>
					</div>
				</div>
				<div class="sqPanel">
	                <div class="sqPanel_head inL">
	                    <div class="btnsBox inLC">
	                    	<span id="spl_rl_add_btn" class="sq-button sq-button-round" onclick="mfrRlPsnAddBtnEvent();">添加</span>
	                    </div>
	                    <span>社会关系/家庭成员</span>
	                </div>
	                <div class="sqPanel_body">
	                    <div id="ligergrid_A1" class="sq-grid"></div>
	                </div>
	            </div>
	            <div class="btnsBox">
	                <span id="saveBtn1" class="sq-button sq-button-round">保存</span>
                    <input name="mId" id="mId" type="hidden" value="${id }">
	            </div>
            </form>
        </div>
        <div tabid="tabB" title="资料信息">
            <iframe frameborder="0" name="tabB" id="tabB" src=""></iframe>
        </div>
        <div tabid="tabC" title="备案账号">
            <iframe frameborder="0" name="tabC" id="tabC" src=""></iframe>
        </div>
		<div tabid="tabE" title="相关项目">
			<iframe frameborder="0" name="tabE" id="tabE" src=""></iframe>
		</div>
    </div>
</div>


<script type="text/javascript">
    var loadedTabs=[];
    var mfrRlPsnGrid;
    var isFlow = '${isFlow}';
    var isModify = '${isModify}';
    var isMdfPayTyp = '${isMdfPayTyp}';
    var mfrId = '${id}';
    var isCheckVerify = '${isCheckVerify}';
    var isDetail = '${isDetail}';

    $(function(){

        var _tkBrCdListJSON = ${tkBrCdListJSON};
        $("#tkBrCd").ligerComboBox({ isShowCheckBox: true, isMultiSelect: true,
            data: _tkBrCdListJSON,
            valueField: 'key',
            textField:'value'
        });
        var _carBrCdListJSON = ${carBrCdListJSON};
        $("#carBrCd").ligerComboBox({ isShowCheckBox: true, isMultiSelect: true,
            data: _carBrCdListJSON,
            valueField: 'key',
            textField:'value'
        });
        var _busBrCdListJSON = ${busBrCdListJSON};
        $("#busBrCd").ligerComboBox({ isShowCheckBox: true, isMultiSelect: true,
            data: _busBrCdListJSON,
            valueField: 'key',
            textField:'value'
        });

        var formSet1  = $("#formSet1").ligerForm({
            validate : true
        });
        $("#formSet1").ligerForm();

        if(isModify && isModify == '1'){
            formSet1.setFieldValidate("payTypCd", {
                required: false,
                select: false
            });
        }else{
            formSet1.setFieldValidate("payTypCd", {
                required: true,
                select: false
            });
        }

        /*formSet1.setFieldValidate("rgnProvCd", {
            required: true,
            select: false
        });
        formSet1.setFieldValidate("rgnCityCd", {
            required: true,
            select: false
        });*/

        $("#saveBtn1").click(function(event) {
            var form = new liger.get("formSet1");
            if (isMdfPayTyp && isMdfPayTyp == '1' && StringUtils.isEmpty($('#payTypCd').val())) {
            	$.ligerDialog.SQalert({content: "请选择放款方式" , SQ_type: "warning" });
		    	return false; //验证不通过，不执行以下代码
            }

            if(isModify && isModify == '1'){
                if (!form.valid()) {
                    $.ligerDialog.SQalert({content:LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
                    return false; //验证不通过，不执行以下代码
                }
			}

            var isValid = SQ.validGrid('ligergrid_A1');
            if ((!isModify || isModify == '1') && isValid == false) {
                $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
                return false;
            }

            var currentOrgCd = '${currentOrgCd}';

            var data = form.getData();
            var id = $("#mId").val();
            data["payTypCdNm"] = $('#payTypCd option:checked').text();
            var mfrRlPsnList = mfrRlPsnGrid.getData();
            data["mfrRlPsnList"] = JSON.stringify(mfrRlPsnList);
            var url='<%=path%>/mfr/mfrBscInfo/add.do';
            if(id){
                url = '<%=path%>/mfr/mfrBscInfo/modify.do';
                data["id"]= id;
            }

            data["rgnPrCd"] = getRgnProvCd();//获取省代码
            data["rgnCyCd"] = getRgnCityCd();//获取市代码
            data["currentOrgCd"] = currentOrgCd;

            data["isModify"] = isModify;//是否修改基本信息
            data["isMdfPayTyp"] = isMdfPayTyp;//是否修改放款方式
            data["isFlow"] = isFlow;//是否流程中

            var wait_dialog;
            $.ajax({
                type: 'post',
                dataType:'json',
                data:data,
                url: url,
                beforeSend: function(){
                    wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
                },
                success:function(msg){
                    wait_dialog.close();
                    if(msg.success == 1){
                        $.ligerDialog.SQalert({content:msg.info, SQ_type: "success"});
                        if(msg.data){
                            $("#mId").val(msg.data);
                        }
                        //callback回调
                        var TAB=top.mainApp.parentTab;
                        var currentTabid=TAB.getSelectedTabItemID();
                        var currentTabiframe=top.$("#"+currentTabid).get(0);
                        var callback=currentTabiframe.W2W.callback;
                        callback();
                        mfrRlPsnGrid.reload();
                    }else{
                        $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
                    }
                }
            })
        })
        initTab();
        initData();
        initMfrRlGrid();
    })
    
    function initData(){
        var id = $("#mId").val();
        var currentOrgCd = '${currentOrgCd}';
        var isFlow = '${isFlow}';
        if(!id){
            return;
        }
        var _url = '<%=path%>/mfr/mfrBscInfo/getById.do';
        $.ajax({
            type: 'get',
            dataType:'json',
            data:{id:id,currentOrgCd:currentOrgCd,isFlow:isFlow},
            url: _url,
            success:function(msg){
                if(msg.success == 1){
                    setData(msg.data);//设置数据
                }else{
                    $.ligerDialog.SQalert({content:msg.info, SQ_type: "error"});
                }
            }
        });
    }
    //设置页面数据
    function setData(data){
        var form = liger.get("formSet1");

		//区域数据处理
        setRgnProvCdUpdataValue(data.rgnPrCd);
        setRgnCityCdUpdataValue(data.rgnCyCd);
        setRegionSelectDataChange();

        $("#mfrNm").val(data.mfrName);
        $("#mfrAddr").val(data.mfrAddr);
        $("#mfrTel").val(data.mfrTel);
        $("#succ").val(data.succ);
        $("#otherInfo").val(data.otherInfo);

        $("#tkBrCd").ligerGetComboBoxManager().selectValue(data.tkBrCdNms);
        $("#tkBrCd").ligerGetComboBoxManager().updateStyle();
        $("#carBrCd").ligerGetComboBoxManager().selectValue(data.carBrCdNms);
        $("#carBrCd").ligerGetComboBoxManager().updateStyle();
        $("#busBrCd").ligerGetComboBoxManager().selectValue(data.busBrCdNms);
        $("#busBrCd").ligerGetComboBoxManager().updateStyle();

        liger.get("payTypCd").setValue(data.payTypCd);
        liger.get("payTypCd").updateStyle();
        if(isFlow && isFlow == '1'){
            if(isModify && isModify == '1'){
                liger.get("payTypCd").setDisabled(true);
			}
			if(isMdfPayTyp && isMdfPayTyp == '1'){
                liger.get("mfrNm").setDisabled(true);
                liger.get("mfrAddr").setDisabled(true);
                liger.get("mfrTel").setDisabled(true);
                liger.get("succ").setDisabled(true);

                //禁用省市
                disabledRgnProvCd();
                disabledRgnCityCd();

                liger.get("tkBrCd").setDisabled(true);
                liger.get("carBrCd").setDisabled(true);
                liger.get("busBrCd").setDisabled(true);

                $("#spl_rl_add_btn").css("display","none");

                $("#otherInfo").setDisabled_ligerTextarea(true);

            }
		}
    };

    //初始化Tab页
    function initTab(){
        pageInTab = $("#pageInTab").ligerTab({
            height: 'auto',
            contextmenu: false,
            minIframeHeight: 450,
            onBeforeSelectTabItem: function (tabid) {
                var _mId = $("#mId").val();
                if(tabid!="tabA"){
                    if(!_mId){
                        top.$.ligerDialog.SQalert({content: "请确认厂商基本信息已存在", SQ_type: "warning"});
                        return false;
                    }
                }
            },
            onAfterSelectTabItem: function(tabid) {
                if (loadedTabs.indexOf(tabid)<0) {
                    var _mId = $("#mId").val();
                    if(tabid!="tabA"){
                        if(!_mId){
                            top.$.ligerDialog.SQalert({content: "请确认厂商基本信息已存在", SQ_type: "warning"});
                            return false;
                        }
                    }
                    loadedTabs.push(tabid);
                    switch (tabid){
                     	case "tabB": //厂商资料
                            pageInTab.setTabItemSrc(tabid, "<%=path%>/mfr/mfrBscInfo/toMfrFileAddPage.do?mfrId="+ mfrId+"&isMdfPayTyp="+isMdfPayTyp+"&isModify="+isModify+"&isDetail="+isModify+"&isCheckVerify="+isCheckVerify+"&isFlow="+isFlow);                     		break;
						case "tabC"://备案账号
                            pageInTab.setTabItemSrc(tabid, "<%=path%>/mfr/mfrbankacc/toListPage.do?mfrId="+ mfrId+"&isMdfPayTyp="+isMdfPayTyp+"&isModify="+isModify+"&isDetail="+isModify+"&isCheckVerify="+isCheckVerify+"&isFlow="+isFlow);
                     		break;
                        case "tabE"://相关项目
                            pageInTab.setTabItemSrc(tabid, "<%=path%>/product/mfrInfo/toMfrPrjBscInfoDetailList.do?mfrId="+_mId);
                            break;
                    }
                };
                SetIframe_height();
            }
        });
    };
    
  	//证件类型
    var certTypCdListJSON = [];
    <c:if test="${not empty certTypCdListJSON}"> certTypCdListJSON = ${certTypCdListJSON}; </c:if>

	//联系人类型
    var rlTypCDListJSON = [];
    <c:if test="${not empty rlTypCDListJSON}"> rlTypCDListJSON = ${rlTypCDListJSON}; </c:if>

    //联系人类型
    var mustAddRlPsnJSON = [];
    <c:if test="${not empty mustAddRlPsnTypCdJSON}">
    	mustAddRlPsnJSON = ${mustAddRlPsnTypCdJSON};
    	console.log("mustAddRlPsnJSON ==="+JSON.stringify(mustAddRlPsnJSON));
    </c:if>

	//初始化主要人员信息
    function initMfrRlGrid() {
    	var _mId = $("#mId").val();
        mfrRlPsnGrid = $("#ligergrid_A1").ligerGrid({
            url: '<%=path%>/mfr/mfrrlpsn/initList.do?',
	    	method : 'get',
	    	parms:{mfrId:_mId},
	    	async: false,
	    	columns: [
					{ display: '联系人姓名(<span class="c_red">*</span>)', name: 'rlNm', width: 150, editor: {type: 'text'},
                        validate: function (value,rowData,col) {
                            if (StringUtils.isEmpty(value) && CollectionUtils.isNotEmpty(mustAddRlPsnJSON) && mustAddRlPsnJSON.indexOf(rowData.rlTypCd) >= 0) return "此项为必填项！";
                            //digits: true       // 是否是数值型或数字
                            //maxlength: 50      // 最大长度
                            //min:1              // 最小长度
                        },maxlength: 50, min:1     // 最大长度
					},
	  	            { display: '联系人类型(<span class="c_red">*</span>)', name: 'rlTypCd', width: 140,
						editor: {
	                       type: 'select',
	                       data: rlTypCDListJSON,
	                       valueField: 'key',
	                       textField: 'value'
						},
						valueField: 'rlTypCd',
						textField: 'rlTypCdNm',
						render: function (item) {
							return item.rlTypCdNm;
						},
						validate: function (value) {
						  if (StringUtils.isEmpty(value)) return "此项为必填项！";
						}
	  	            },
	  	            { display: '证件类型(<span class="c_red">*</span>)', name: 'certTypCd', width: 140,
						editor: {
	                       type: 'select',
	                       data: certTypCdListJSON,
	                       valueField: 'key',
	                       textField: 'value'
						},
	                   	valueField: 'certTypCd',
	                   	textField: 'certTypCdNm',
	                   	render: function (item) {
	                   	    return item.certTypCdNm;
	                  	},
                        validate: function (value,rowData,col) {
                            if (StringUtils.isEmpty(value) && CollectionUtils.isNotEmpty(mustAddRlPsnJSON) && mustAddRlPsnJSON.indexOf(rowData.rlTypCd) >= 0) return "此项为必填项！";
                        }
	  	            },
	  	            { display: '证件号(<span class="c_red">*</span>)', name: 'certNo', width: 150, editor: {type: 'text'},
                        validate: function (value,rowData,col) {
                            if (StringUtils.isEmpty(value) && CollectionUtils.isNotEmpty(mustAddRlPsnJSON) && mustAddRlPsnJSON.indexOf(rowData.rlTypCd) >= 0) return "此项为必填项！";
                        },digits: true,maxlength: 20, min:15
                    },
	                { display: '联系电话(<span class="c_red">*</span>)', name: 'tel', width: 150, editor: {type: 'text'},
                        validate: function (value,rowData,col) {
                            if (StringUtils.isEmpty(value) && CollectionUtils.isNotEmpty(mustAddRlPsnJSON) && mustAddRlPsnJSON.indexOf(rowData.rlTypCd) >= 0) return "此项为必填项！";
                        },digits: true,maxlength: 20, min:8
					},
					{ display: '备注', name: 'rlRem', minWidth: 150, editor: {type: 'text'}},
	   	  	        { display: '操作', minWidth: 80 , align:'left',
		            	render: function (rowdata, rowindex, value){
		                	var d = "";
                            if (rowdata.isDefault != 1 || ((!isMdfPayTyp || isMdfPayTyp != '1') && (!isModify || isModify != '1'))) {
                                if (rowdata.id) {
                                    d = '<a class="sq-button-hasicon" href="javascript:deleteDB(';
                                    d += "'" + rowdata.__id + "'";
                                    d += ",'" + rowdata.id + "'";
                                    d += ')"><i class="sqBtnIco_7"></i>删除</a>';
                                } else {
                                    d = '<a class="sq-button-hasicon" href="javascript:deletePage(';
                                    d += "'" + rowdata.__id + "'";
                                    d += ')"><i class="sqBtnIco_7"></i>删除</a>';
                                }
                            }
		                    return d;
		            	}
					}],
	   	  	   usePager: false,
	           root: 'rows',
	           record: 'total',
	           hideLoadButton: true,
	           width: '100%',
	           height: 300,
	           heightDiff: -2,
			   <c:if test="${not empty isMdfPayTyp and isMdfPayTyp == '1'}">
			   enabledEdit: false,
			   </c:if>
			   <c:if test="${empty isMdfPayTyp or isMdfPayTyp != '1'}">
			   enabledEdit: true,
			   </c:if>
	           isScroll: false,
	           checkbox: false,
	           rownumbers: true,
	           allowUnSelectRow: true,
	           alternatingRow: false,
	           enabledSort:false,
	           onAfterShowData: function() {
	           	SetIframe_height ();
	           }
	        });
	    };

    //数据库删除联系人信息
    function deleteDB(rowdata_id, _id) {
        //删除数据库
        var _ids = [];
        _ids.push(_id);
        $.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
            if (flag == false) return;
            var wait_dialog;
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: {ids: JSON.stringify(_ids)},
                url: '<%=path%>/mfr/mfrrlpsn/removeByIds.do',
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                },
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                        mfrRlPsnGrid.reload();
                    } else {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                }
            });
        });
    }

    //本地删除
    function deletePage(rowdata_id) {
        $.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
            if (flag == false) return;
            //删除页面
            mfrRlPsnGrid.deleteRow(rowdata_id);
        })
    }

    //联系人新增
    function mfrRlPsnAddBtnEvent() {
        var manager = $("#ligergrid_A1").ligerGetGridManager();
        manager.addRow({
            rlNm: '',
            rlTypCd: '',
            certTypCd: '',
            certNo: '',
            tel: '',
            rlRem: ''
        });
    };
</script>



</body>
</html>
